<!-- 系统配置 -->
<template>
  <div class="app-container">    
    <!-- 测试 -->
    <div class="mt20">
      <el-button @click="addSection">
        <i-ep-refresh />
        新增
      </el-button>
      <el-table
        v-loading="loadingNew"
        :data="pageDataNew"
        @selection-change="handleSelectionChangeNew"
        class="mt20"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column
          key="id"
          label="编号"
          align="center"
          prop="id"
          width="80"
        />
        <el-table-column
          key="username"
          label="用户名"
          align="center"
          prop="username"
        />
        <el-table-column label="用户昵称" align="center" prop="nickname" />
        <el-table-column
          label="性别"
          width="100"
          align="center"
          prop="genderLabel"
        />

        <el-table-column
          label="部门"
          width="150"
          align="center"
          prop="deptName"
        />
        <el-table-column
          label="手机号码"
          align="center"
          prop="mobile"
          width="120"
        />
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">
              {{ scope.row.status == 1 ? "正常" : "禁用" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        />
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button
              v-hasPerm="['sys:user:password:reset']"
              type="primary"
              size="small"
              link
              @click="hancleResetPassword(scope.row)"
            >
              <i-ep-refresh-left />
              重置密码
            </el-button>
            <el-button
              v-hasPerm="['sys:user:edit']"
              type="primary"
              link
              size="small"
              @click="handleOpenDialog(scope.row.id)"
            >
              <i-ep-edit />
              编辑
            </el-button>
            <el-button
              v-hasPerm="['sys:user:delete']"
              type="danger"
              link
              size="small"
              @click="handleDelete(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        v-model="dialogNew.visible"
        :title="dialogNew.title"
        width="800px"
        @close="handleCloseDialogNew"
      >
        <el-form
          ref="dataFormRef"
          :model="formDataNew"
          :rules="rulesDataNew"
          label-width="100px"
          style="width: 400px;"
        >
          <el-form-item label="编号:" prop="id">
            <el-input
              v-model="formDataNew.id"
              placeholder="请输入编号"
              :maxlength="50"
            />  
          </el-form-item>
          <el-form-item label="用户名:" prop="username">
            <el-input
              v-model="formDataNew.username"
              placeholder="请输入用户名"
              :maxlength="50"
            />  
          </el-form-item>
          <el-form-item label="用户昵称:" prop="nickname">
            <el-input
              v-model="formDataNew.nickname"
              placeholder="请输入用户昵称"
              :maxlength="50"
            />  
          </el-form-item>
          <el-form-item label="性别:" prop="gender">
            <el-select
              v-model="formDataNew.gender"
              placeholder="请选择性别"
              clearable
            >
              <el-option v-for="(item, index) in genderList" :key="index" :label="item.text" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="部门:" prop="deptId">
            <el-select
              v-model="formDataNew.deptId"
              placeholder="请选择部门"
              clearable
            >
              <el-option v-for="(item, index) in deptNameList" :key="index" :label="item.text" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="手机号码:" prop="phone">
            <el-input
              v-model="formDataNew.phone"
              placeholder="请输入手机号码"
              :maxlength="50"
            />  
          </el-form-item>
          <el-form-item>
            <el-button
              type="danger"
              size="small"
              @click="submit"
            >
              提交
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "Config",
  inheritAttrs: false,
});
import ConfigAPI, {
  ConfigPageVO,
} from "@/api/config";
import UserAPI, {  UserPageQuery } from "@/api/user";
const idsNew = ref<number[]>([]);
const loadingNew = ref(false);
const queryParamsNew = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
});
const dialogNew = reactive({
  title: "",
  visible: false,
})
const pageDataNew = ref<ConfigPageVO[]>([])
const validateUsername = (rules, value, callback) => {
  const reg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
  if(!value){
    callback(new Error('请输入手机号'))
  }else if(!reg.test(value)) {
    callback(new Error('请输入正确的手机号'))
  }else{
    callback()
  }
}
const rulesDataNew = reactive({
  id: [
    { required: true, message: "请输入编号", trigger: "blur" },
  ],
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
  ],
  nickname: [
    { required: true, message: "请输入用户昵称", trigger: "blur" },
  ],
  gender: [
    { required: true, message: "请选择性别", trigger: "change" },
  ],
  deptId: [
    { required: true, message: "请选择部门", trigger: "change" },
  ],
  phone: [
    { required: true, validator: validateUsername, trigger: "blur" },
  ],
})
const formDataNew = reactive({
  id: null,
  username: null,
  nickname: null,
  gender: null,
  deptId: null,
  phone: null,
})
const genderList = ref([
  {
    text: '男',
    value: 1,
  },
  {
    text: '女',
    value: 2,
  },
  {
    text: '未知',
    value: 3,
  }
])
const deptNameList = ref([
  {
    text: '又来技术',
    value: 1,
  },
  {
    text: '有道技术',
    value: 2,
  },
  {
    text: '测试部门',
    value: 3,
  }
])
const dataFormRef = ref(ElForm);
function handleSelectionChangeNew(selection: any) {
  idsNew.value = selection.map((item: any) => item.id)
}
function handleCloseDialogNew(){
  dialogNew.visible = false
}
function handleQueryNew() {
  UserAPI.getPage(queryParamsNew).then((res) => {
    if(res.list && res.list.length > 0){
      pageDataNew.value = res.list
    }
  });
}
function addSection(){
  dialogNew.visible = true
}
function submit(){
  dataFormRef.value.validate((vaild: any) => {
    if(vaild){
      formDataNew.createTime = add(10000000, 20000000)
      pageDataNew.value.push(formDataNew)
      dialogNew.visible = false      
    }
  }) 
}

function add(num1:any, num2:any){
  return num1 + num2
}
onMounted(() => {
  handleQueryNew();
});
</script>
<style lang="scss" scope>
  .flexClass{
    display: flex;
  }
  ::v-deep .el-form-item__content{
    width: 100%;
  }
  // /deep/ .el-form-item__content{
  //   width: 100%;
  // }
  .ml10{
    margin-left: 10px;
  }
  .mb10{
    margin-bottom: 10px;
  }
  .mt20{
    margin-top: 20px;
  }
</style>
